
<#
.SYNOPSIS 
Hydration Script for MDTEx

.DESCRIPTION
_TEMPLATE_

.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

.LINK
http://MDTEx.CodePlex.com

#>

[CmdletBinding()]
param(
    [parameter(mandatory=$true,HelpMessage="Create a new Deployment Share.")]
    [switch] $CreateNewDeploymentShare,

    [parameter(mandatory=$true,HelpMessage="Location of Local Deployment Share.")]
    [string] $DeploymentLocalPath, # Example: c:\DeploymentShare

    [parameter(mandatory=$true,HelpMessage="Location of Local Deployment Share from network.")]
    [string] $DeploymentNetShare, #Example: DeploymentShare$

    [parameter(mandatory=$true,HelpMessage="Location of Local Deployment Share from network.")]
    [string] $DeploymentName,  # Example: 

    [string] $DPDrive = "DS001",

    [string] $SourcePath = "$PsScriptROot\..\..\templates\Distribution",

    [parameter(ValueFromRemainingArguments=$true)] $Remaining

)

if (-not $CreateNewDeploymentShare) { exit }

"### Create a new Deployment Folder" | Write-LogVerbose

if ( ! (Test-Path $DeploymentLocalPath ) )
{
    "Clean Persistent Shares" | Write-LogVerbose
    Get-MDTPersistentDrive | Where-Object Name -eq $DPDrive | Where-Object Path -eq $DeploymentLocalPath | %{ Remove-MDTPersistentDrive -name $_.Name }

    "Create Local Deployment Path: $DeploymentLocalPath" |write-LogVerbose
    new-item -Path $DeploymentLocalPath -type directory -Force |out-string |write-LogVerbose
    icacls $DeploymentLocalPath /grant "NT AUTHORITY\Authenticated Users:(OI)(CI)(RX)" |out-string |write-LogVerbose
}

"### Create a new Deployment Share" | Write-LogVerbose

if (-not (gwmi win32_share -filter "name='$DeploymentNetShare'") )
{
    "Create Share $DeploymentNetShare=$DeploymentLocalPath /GRANT:NT Authority\Authenticated Users,Full" | out-string |write-LogVerbose
    net share "$DeploymentNetShare=$DeploymentLocalPath" "/GRANT:NT Authority\Authenticated Users,Full" |out-string |write-LogVerbose
}

"### Create new MDT share $DPDrive [$DeploymentLocalPath] [$DeploymentName]" |write-LogVerbose
New-PSDrive -Name $DPDrive -PSProvider MDTProvider -Scope global -Root $DeploymentLocalPath -description $DeploymentName | add-MDTPersistentDrive |out-string |write-LogVerbose

"Add MDT Source Share DS00S $SourcePath" | write-logVerbose
new-PSDrive -Name DS00S -PSProvider MDTProvider -Root $SourcePath -Description "MDT Source Share" -scope Script |out-string |write-Logverbose

foreach ( $Folder in "Applications", "Operating Systems", "Out-of-Box Drivers", "Packages", "Task Sequences", "Selection Profiles" )
{
    "Copy [DS00S`:\$Folder] to [$($DPDrive)`:\$Folder]" | Write-Log
    Copy-Item -Path "DS00S`:\$Folder" -Destination "$($DPDrive)`:\$Folder" -Recurse | Out-String |Write-Verbose
}

foreach ( $File in "CustomSettings.ini","BootStrap.ini","settings.xml")
{
    "Copy [$File]" | Write-LogVerbose
    copy-item "$SourcePath\Control\$File" "$DeploymentLocalPath\Control" | write-verbose
}

foreach ( $directory in "scripts","tools","winpe.x86","winpe.x64", "`$OEM`$" )
{
    copy-item -recurse "$SourcePath\$directory\*" "$DeploymentLocalPath\$directory" -Force -ErrorAction SilentlyContinue
}

